package com.zrrd.learn.chapter07字符串;

/**
 * 字符串练习题
 *
 * @author wrn
 * @version 1.0
 * @date 2023/7/18 9:08
 */

public class StringExercise02 {

    /**
     * 设计一个方法，将给定的字符串每一个首字母大写 “this is a test of java”---->“This Is A Test Of Java”
     * @param str
     */
    public static void question01(String str) {

        System.out.println("第一题");
        char[] cstr = str.toCharArray();
        cstr[0] = Character.toUpperCase(cstr[0]);
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == ' ') {
                cstr[i+1] = Character.toUpperCase(cstr[i+1]);
            }
        }
        System.out.println(cstr);

    }

    /**
     * 设计一个方法，获取给定字符串中的全部数字 “za1o1zbp24tcq”
     * @param str
     */
    public static void question02(String str) {

        System.out.println("第二题");
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (ch>='0'&&ch<='9'){
                System.out.print(ch);
            }
        }
        System.out.println();
    }

    /**
     * 编写程序在将字符串 “want you to know one thing”, 统计出字母 “n”和 字母 “o”的出现次数
     * @param str
     * @param a
     * @param b
     */
    public static void question03(String str, char a ,char b){
        System.out.println("第三题");
        int count1 = 0;
        int count2 = 0;
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (ch==a){
                count1++;
            }
            if (ch==b){
                count2++;
            }
        }
        System.out.println("字符串中字符"+a+"出现的次数为"+count1+"\n字符"+b+"出现的次数"+count2);
    }

    public static void main(String[] args) {

        question01("this is a test of java");

        question02("za1o1zbp24tcq");

        question03("want you to know one thing",'n','o');
    }
}
